﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace WindowsFormsApplication1.FloorUIForms
{
    public partial class FloorUI3 : Form, FloorUI
    {
        string namingConvention;
        Elevator elevator;

        ArrayList allCommands; 
        ArrayList firstFloorCommands;
        ArrayList secondFloorCommands;
        ArrayList thirdFloorCommands;

        public FloorUI3(String floorNaming, Elevator e)
        {
            InitializeComponent();
            namingConvention = floorNaming;
            elevator = e;

            allCommands = new ArrayList(); 
            firstFloorCommands = new ArrayList();
            secondFloorCommands = new ArrayList();
            thirdFloorCommands = new ArrayList(); 

            // Sets the naming of floors to either US or UK
            setFloorNaming();

            // Sets the current floor border
            borderCurrentFloorofElevator(elevator.getCurrentFloor());

            // Removes access to the floors below minimum floor number
            setupMinimumFloor();
        }

        private void setupMinimumFloor()
        {
            if (elevator.getMinFloor() == 1)
            {
            }
            else if (elevator.getMinFloor() == 2)
            {
                upBttnFirst.Hide();
                downBttnSecond.Hide(); 
            }
        }

        private void addNewCommand(String command)
        {
            if (allCommands.Contains(command))
            {
            }
            else
            {
                allCommands.Add(command);
            }
        }

        private void addNewFirstFloorCommand(String command)
        {
            if (firstFloorCommands.Contains(command))
            {
            }
            else
            {
                firstFloorCommands.Add(command); 
            }
        }

        private void addSecondFloorCommand(String command)
        {
            if (secondFloorCommands.Contains(command))
            {
            }
            else
            {
                secondFloorCommands.Add(command); 
            }
        }

        private void addThirdFloorCommand(String command)
        {
            if (thirdFloorCommands.Contains(command))
            {
            }
            else
            {
                thirdFloorCommands.Add(command); 
            }
        }

        private void removeCommand(String list, String command)
        {
            if (list.Equals("allCommands"))
            {
                allCommands.Remove(command); 
            }
            else if (list.Equals("firstFloorCommands"))
            {
                firstFloorCommands.Remove(command); 
            }
            else if (list.Equals("secondFloorCommands"))
            {
                secondFloorCommands.Remove(command); 
            }
            else if (list.Equals("thirdFloorCommands"))
            {
                thirdFloorCommands.Remove(command); 
            }
        }

        private String getNextCommand()
        {
            if (allCommands.Count != 0)
            {
                return allCommands[0].ToString();
            }
            else
            {
                return "";
            }
        }

        private void setFloorNaming()
        {
            if (namingConvention.Equals("UK"))
            {
                firstFloorLbl.Text = "G";
                secondFloorLbl.Text = "1";
                thirdFloorLbl.Text = "2"; 
            }
            else if (namingConvention.Equals("US"))
            {
                firstFloorLbl.Text = "1";
                secondFloorLbl.Text = "2";
                thirdFloorLbl.Text = "3"; 
            }
        }

        private void borderCurrentFloorofElevator(int floorNumber)
        {
            if (floorNumber == 1)
            {
                firstFloorPanel.BorderStyle = BorderStyle.FixedSingle;
            }
            else if (floorNumber == 2)
            {
                secondFloorPanel.BorderStyle = BorderStyle.FixedSingle;
            }
            else if (floorNumber == 3)
            {
                thirdFloorPanel.BorderStyle = BorderStyle.FixedSingle; 
            }
        }

        public void lightUpBttn(String floorNumber)
        {
            if (floorNumber.Equals("firstup"))
            {
                upBttnFirst.Image = Properties.Resources.up_button_small_lit;
            }
            else if (floorNumber.Equals("secondup"))
            {
                upBttnSecond.Image = Properties.Resources.up_button_small_lit;
            }
            else if (floorNumber.Equals("seconddown"))
            {
                downBttnSecond.Image = Properties.Resources.down_button_small_lit;
            }
            else if (floorNumber.Equals("thirddown"))
            {
                downBttnThird.Image = Properties.Resources.down_button_small_lit;
            }
        }

        public void lightOutBttn(String floorNumber)
        {
        }

        public void moveLift(String floorNumber)
        {
        }

        private void upBttnFirst_Click(object sender, EventArgs e)
        {
            allCommands.Add("up1"); 
            firstFloorCommands.Add("up1"); 

            //elevator.addNewCommand("up1");
            //Prob should make this update all at once on timer
            upBttnFirst.Image = Properties.Resources.up_button_small_lit;
            //new UCC.UCCPressUpButton(this, "firstup").Run(); 
        }

        private void downBttnSecond_Click(object sender, EventArgs e)
        {
            allCommands.Add("down2"); 
            secondFloorCommands.Add("down2"); 


            //elevator.addNewCommand("downtwo");
            downBttnSecond.Image = Properties.Resources.down_button_small_lit;
            //new UCC.UCCPressDownButton(this, "seconddown").Run(); 
        }

        private void upBttnSecond_Click(object sender, EventArgs e)
        {
            allCommands.Add("up2"); 
            secondFloorCommands.Add("up2"); 

            //elevator.addNewCommand("up2");
            upBttnSecond.Image = Properties.Resources.up_button_small_lit;
            //new UCC.UCCPressUpButton(this, "secondup").Run(); 
        }

        private void downBttnThird_Click(object sender, EventArgs e)
        {
            allCommands.Add("down3"); 
            thirdFloorCommands.Add("down3"); 

            //elevator.addNewCommand("downthree");
            downBttnThird.Image = Properties.Resources.down_button_small_lit;
            //new UCC.UCCPressDownButton(this, "thirddown").Run(); 
        }

        private void elevatorTimer_Tick(object sender, EventArgs e)
        {
            elevator.sortCommands(); 
            String command = elevator.getNextCommand(); 
            if (command.Equals("up1"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500); 
                    elevator.setCurrentFloor(1);
                    upBttnFirst.Image = Properties.Resources.up_button_small;
                    elevator.removeCommand("up1");
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving down");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(1);
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving down");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
            }
            else if (command.Equals("downtwo"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving up"); 
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                    downBttnSecond.Image = Properties.Resources.down_button_small;
                    elevator.removeCommand("downtwo");

                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving down");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
            }
            else if (command.Equals("up2"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving up");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                    upBttnSecond.Image = Properties.Resources.up_button_small;
                    elevator.removeCommand("up2");
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving down");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
            }
            else if (command.Equals("downthree"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving up");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving up");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(3);
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(3);
                    downBttnThird.Image = Properties.Resources.down_button_small;
                    elevator.removeCommand("downthree");
                }
                if (command.Equals("downthree") || command.Equals("up1"))
                {
                    elevator.sortCommands(); 
                }
            }
        }

        private void GUIUpdateTimer_Tick(object sender, EventArgs e)
        {
            if (elevator.getDoorStatus().Equals("open"))
            {
                if (elevator.getCurrentFloor() == 1)
                {                  
                    floorOneDoors.Image = Properties.Resources.doors_open_small;
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    floorTwoDoors.Image = Properties.Resources.doors_open_small;
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    floorThreeDoors.Image = Properties.Resources.doors_open_small;

                }
            }
            else if (elevator.getDoorStatus().Equals("closed"))
            {
                floorOneDoors.Image = Properties.Resources.doors_closed_small;
                floorTwoDoors.Image = Properties.Resources.doors_closed_small;
                floorThreeDoors.Image = Properties.Resources.doors_closed_small;
            }
            if (elevator.getCurrentFloor() == 1)
            {
                firstFloorPanel.BorderStyle = BorderStyle.FixedSingle;
                secondFloorPanel.BorderStyle = BorderStyle.None;
                thirdFloorPanel.BorderStyle = BorderStyle.None;
            }
            else if (elevator.getCurrentFloor() == 2)
            {
                firstFloorPanel.BorderStyle = BorderStyle.None;
                secondFloorPanel.BorderStyle = BorderStyle.FixedSingle;
                thirdFloorPanel.BorderStyle = BorderStyle.None;
            }
            else if (elevator.getCurrentFloor() == 3)
            {
                firstFloorPanel.BorderStyle = BorderStyle.None;
                secondFloorPanel.BorderStyle = BorderStyle.None;
                thirdFloorPanel.BorderStyle = BorderStyle.FixedSingle;
            }
        }

        private void elevatorChecker_Tick(object sender, EventArgs e)
        {
            String command = getNextCommand();
            if (command.Equals("up1"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(1);
                    upBttnFirst.Image = Properties.Resources.up_button_small;
                    removeCommand("allCommands", "up1");
                    removeCommand("firstFloorCommands", "up1"); 
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    if (secondFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving down");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(1);
                    }
                    else if (secondFloorCommands.Contains("down2"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                        downBttnSecond.Image = Properties.Resources.down_button_small;
                        removeCommand("allCommands", "down2");
                        removeCommand("secondFloorCommands", "down2");
                    }
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    if (thirdFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving down");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                    }
                    else if (thirdFloorCommands.Contains("down3"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(3);
                        downBttnThird.Image = Properties.Resources.down_button_small;
                        removeCommand("allCommands", "down3");
                        removeCommand("thirdFloorsCommands", "down3"); 
                    }
                }
            }
            else if (command.Equals("down2"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    if (firstFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving up");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                    }
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                    downBttnSecond.Image = Properties.Resources.down_button_small;
                    removeCommand("allCommands", "down2");
                    removeCommand("secondFloorCommands", "down2"); 
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("closed");
                    elevator.setMovingDirection("moving down");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2); 
                }
            }
            else if (command.Equals("up2"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    if (firstFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving up");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                    }
                    else if (firstFloorCommands.Contains("up1"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(1);
                        upBttnFirst.Image = Properties.Resources.up_button_small;
                        removeCommand("allCommands", "up1");
                        removeCommand("firstFloorCommands", "up1"); 
                    }
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(2);
                    upBttnSecond.Image = Properties.Resources.up_button_small;
                    removeCommand("allCommands", "up2");
                    removeCommand("secondFloorCommands", "up2"); 
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    if (thirdFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving down");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                    }
                    else if (thirdFloorCommands.Contains("down3"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(3);
                        downBttnThird.Image = Properties.Resources.down_button_small;
                        removeCommand("allCommands", "down3");
                        removeCommand("thirdFloorCommands", "down3"); 
                    }
                }
            }
            else if (command.Equals("down3"))
            {
                if (elevator.getCurrentFloor() == 1)
                {
                    if (firstFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving up");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                    }
                    else if (firstFloorCommands.Contains("up1"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(1);
                        upBttnFirst.Image = Properties.Resources.up_button_small;
                        removeCommand("allCommands", "up1");
                        removeCommand("firstFloorCommands", "up1");
                    }
                }
                else if (elevator.getCurrentFloor() == 2)
                {
                    if (secondFloorCommands.Count == 0)
                    {
                        elevator.setDoors("closed");
                        elevator.setMovingDirection("moving up");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(3);
                    }
                    else if (secondFloorCommands.Contains("up2"))
                    {
                        elevator.setDoors("open");
                        elevator.setMovingDirection("waiting");
                        Thread.Sleep(500);
                        elevator.setCurrentFloor(2);
                        upBttnSecond.Image = Properties.Resources.up_button_small;
                        removeCommand("allCommands", "up2");
                        removeCommand("secondFloorCommands", "up2");
                    }
                }
                else if (elevator.getCurrentFloor() == 3)
                {
                    elevator.setDoors("open");
                    elevator.setMovingDirection("waiting");
                    Thread.Sleep(500);
                    elevator.setCurrentFloor(3);
                    downBttnThird.Image = Properties.Resources.down_button_small;
                    removeCommand("allCommands", "down3");
                    removeCommand("thirdFloorCommands", "down3");
                }
            }
        }
    }
}
